package java课题设计.功能类.Seek;
import java课题设计.工具类.Seek;
import java课题设计.用户类包.Student;
 
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.io.IOException;
//此类用于按照学生学号进行搜索学生
public class Seekid extends JFrame implements MouseListener {
    Font f=new Font("宋体",Font.BOLD,25);
    String id;
    private JFrame pre;
    private JTable table;
    JButton seek=new JButton("查找");
    public Seekid(JFrame pre) {
        extracted();
        initImage();
        this.setVisible(true);
    }
    public void extracted(){
        //设置登录界面的长和宽
        this.setSize(500,450);
        this.setTitle("请输入你要查找的学生的学号");
        this.setAlwaysOnTop(true);
        this.setDefaultCloseOperation(3);
        this.setLayout(null);
        this.setLocationRelativeTo(null);
 
        //绑定监听器
        seek.addMouseListener(this);
 
    }
    public void initImage(){
        //添加输入学号提示框
        JLabel ID=new JLabel("请输入你要查找的学号");
        ID.setFont(f);
        ID.setBounds(75,40,400,100);
        this.getContentPane().add(ID);
 
 
        //输入学号输入文本框
        JTextField IDtext=new JTextField();
        IDtext.setBounds(75,140,250,50);
        this.getContentPane().add(IDtext);
        //添加监听器
        IDtext.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void insertUpdate(DocumentEvent e) {
                id = IDtext.getText();
            }
 
            @Override
            public void removeUpdate(DocumentEvent e) {
                id = IDtext.getText();
            }
 
            @Override
            public void changedUpdate(DocumentEvent e) {
                id = IDtext.getText();
            }
        });
 
        //添加查找按钮
        seek.setFont(f);
        seek.setBounds(100,220,140,45);
        this.getContentPane().add(seek);
 
    }
    public void chazhao(String id) throws IOException {
        boolean flag=Seek.seek3(id);
        if(flag==false){
            //说明找到了学号为id的学生
            System.out.println("找到了");
            //创建表格将其显示出来
            String student=Seek.seek2(id);
            // 创建表格
            String[] columnNames = {"学生姓名","性别", "学号", "体育成绩", "Java成绩", "高数成绩", "出生年月日"};
            DefaultTableModel model = new DefaultTableModel(columnNames, 10);
            table = new JTable(model);
            //将取出的数据进行分割
            String[] fields = student.split("\t");
            String name = fields[0];
            String sex = fields[1];
            String sid = fields[2];
            double PEscore = Double.parseDouble(fields[3]);
            double Javascore = Double.parseDouble(fields[4]);
            double Mathscore = Double.parseDouble(fields[5]);
            String birthDate = fields[6];
 
            //将学生数据添加到表格中
            Object[] rowData = {name,sex, id, String.format("%.0f", PEscore), String.format("%.0f", Javascore), String.format("%.0f", Mathscore), birthDate};
            model.setValueAt(name, 1, 0);
            model.setValueAt(sex,1,1);
            model.setValueAt(id, 1, 2);
            model.setValueAt(String.format("%.0f", PEscore), 1, 3);
            model.setValueAt(String.format("%.0f", Javascore), 1, 4);
            model.setValueAt(String.format("%.0f", Mathscore), 1, 5);
            model.setValueAt(birthDate, 1, 6);
 
            // 创建 JScrollPane
            JScrollPane scrollPane = new JScrollPane(table);
 
            // 创建 JFrame 并展示 UI 界面
            JFrame frame = new JFrame("学生信息表");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLocationByPlatform(true);
 
            JPanel contentPane = new JPanel(new BorderLayout());
            contentPane.add(scrollPane, BorderLayout.CENTER);
            frame.setContentPane(contentPane);
 
            frame.pack();
            frame.setVisible(true);
        }
        else{
            //跳出弹窗查找失败
            System.out.println("查找失败");
            JDialog wrong=new JDialog(this,null,true);
            wrong.setBounds(550,300,300,200);
            wrong.setTitle("未找到该学生");
            JLabel w=new JLabel("未找到该学生");
            w.setFont(new Font("宋体",Font.BOLD,18));
            w.setBounds(100,100,100,100);
            wrong.add(w);
            wrong.setAlwaysOnTop(true);
            wrong.setVisible(true);
 
        }
    }
    @Override
    protected void processWindowEvent(WindowEvent e){
        super.processWindowEvent(e);
        if(e.getID()==WindowEvent.WINDOW_CLOSED){
            this.setVisible(false);//隐藏当前窗口
            pre.setVisible(true);//显示上一级窗口
        }
    }
    @Override
    public void mouseClicked(MouseEvent e) {
        if(e.getSource()==seek){
            System.out.println("点击了查找按钮");
            try {
                chazhao(id);
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        }
    }
 
    @Override
    public void mousePressed(MouseEvent e) {
 
    }
 
    @Override
    public void mouseReleased(MouseEvent e) {
 
    }
 
    @Override
    public void mouseEntered(MouseEvent e) {
 
    }
 
    @Override
    public void mouseExited(MouseEvent e) {
 
    }
}
